<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use app\common\model\RoleMenu;
use fast\Random;
use think\Config;
use think\Db;
use think\Validate;

/**
 * 会员接口
 */
class Menu extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = '*';

    public function navigate()
    {
        $userinfo=$this->auth->getUserinfo();

        $role_id=$userinfo['role_id'];

        $menuTree = $this->getRoleMenu($role_id);
        $this->success('success', $menuTree);
    }


    public function getRoleMenu($roleIds){
        // 4. 查询该角色下所有菜单
        $menuIds = RoleMenu::where('role_id', $roleIds)->column('menu_id');
// 5. 查询菜单详情
        $menus = Db::name('menu')
            ->where('is_hide',0)
            ->where('id', 'in', $menuIds)
            ->order('id asc')
            ->select();

        // // 6. 添加固定的个人信息菜单
        // $personalCenterMenu = [
        //     'id' => 59,
        //     'parent_id' => 6,
        //     'name' => 'personalCenter',
        //     'title' => '个人信息',
        //     'path' => 'personalCenter',
        //     'component' => 'setting/personalCenter/index',
        //     'icon' => '',
        //     'sort' => 0,
        //     'hidden' => 1,
        //     'created_at' => '2025-07-19 13:57:08',
        //     'updated_at' => '2025-07-19 14:16:57'
        // ];
        $newmenu=Db::name('menu')
        ->where('id', 'in', [59,62,63])
        ->order('id asc')
        ->select();
        
        // 将个人信息菜单添加到菜单列表中
        $menus = array_merge($menus,$newmenu);

        // 7. 组装菜单树
        $menuTree = $this->buildMenuTree($menus);
        
        return $menuTree;
    }

    private function buildMenuTree($menus, $parentId = 0)
    {
        $tree = [];
        foreach ($menus as $menu) {
            if ($menu['parent_id'] == $parentId) {
                $children = $this->buildMenuTree($menus, $menu['id']);
                $node = [
                    'path' => $menu['path'],
                    'component' => $menu['component'],
                    'name' => $menu['name'],
                    'hidden' => $menu['hidden']==1?true:false,
                    'meta' => [
                        'title' => $menu['title'],
                        'icon' => $menu['icon'],
                        // 你可以加权限、actions等
                    ],
                ];
                if ($children) {
                    $node['children'] = $children;
                }
                $tree[] = $node;
            }
        }

        
        return $tree;
    }

    /**
     * 智能填充redirect字段
     */
    public function updateMenuRedirects()
    {
        // 获取所有菜单
        $menus = Db::name('menu')->select();

        // 先找出所有有子菜单的父菜单ID
        $parentIds = array_unique(array_column($menus, 'parent_id'));
        $hasChildren = array_count_values(array_column($menus, 'parent_id'));

        foreach ($menus as $menu) {
            $update = [];

            // 情况1：是父菜单(有子菜单)且redirect为空
            if (isset($hasChildren[$menu['id']]) && $menu['redirect'] === null) {
                $update['redirect'] = 'noRedirect';
            }
            // 情况2：是顶级路由(如首页)需要特殊redirect
            elseif ($menu['path'] === '/' && $menu['redirect'] === null) {
                $update['redirect'] = '/index';
            }
            // 情况3：是Layout组件但没有设置redirect
            elseif ($menu['component'] === 'Layout' && $menu['redirect'] === null) {
                $update['redirect'] = 'noRedirect';
            }

            if (!empty($update)) {
                Db::name('menutest')
                    ->where('id', $menu['id'])
                    ->update($update);
            }
        }
        return count($menus);
    }


}
